/**************************************************
 * frame webui's splitor
 * autor:bill 2005.11
 **************************************************/
function FrameSplitor(frameSetID,frameIndex,dispType,reduceType,minSize)
{ 
  //属性
  this.showFlag=FrameSplitor.YESNO_YES;
  this.frameSetID=frameSetID;
  this.frameIndex=frameIndex;
  this.dispType=dispType;
  this.reduceType=reduceType;
  this.minSize=minSize;
  this.isLockMove=false;

  //dyna data
  this.startScreenX=0;
  this.startScreenY=0;
  this.frameSetObj=null;

  this.origSizeArr=null;
  this.isStarArr=null;
  this.preSizeArr=null;
  this.curSizeArr=null;
};

FrameSplitor.DISPTYPE_HORIZONTAL=1;
FrameSplitor.DISPTYPE_VERTICAL=2;

FrameSplitor.YESNO_YES=1;
FrameSplitor.YESNO_NO=0;

FrameSplitor.REDUCE_TYPE_NONE=0;
FrameSplitor.REDUCE_TYPE_PRE=1;
FrameSplitor.REDUCE_TYPE_POST=2;


FrameSplitor.prototype.init=function()
{
  this.frameSetObj=window.parent.document.getElementById(this.frameSetID);
  var counter=0;
  this.origSizeArr=this.getSizeArr();
  this.preSizeArr=this.origSizeArr;
  var rowsCols="";
  if(this.dispType==FrameSplitor.DISPTYPE_HORIZONTAL)
    rowsCols=this.frameSetObj.cols;
  else
    rowsCols=this.frameSetObj.rows;
  var arr=rowsCols.split(",");
  this.isStarArr=new Array();
  var hasStar=false;
  for(var i=0;i<arr.length;i++)
  {
    if(!hasStar && arr[i].indexOf("*")!=-1)
    {
      this.isStarArr[i]=true;
      hasStar=true;
    }
    else
      this.isStarArr[i]=false;
    if(i==arr.length-1 && !hasStar)
      this.isStarArr[i]=true;
  }
};

//取frame的宽度或高度集。
FrameSplitor.prototype.getSizeArr=function()
{
  var counter=0;
  var sizeArr=new Array();
  var children=this.frameSetObj.childNodes;
  for(var i=0;i<children.length;i++)
  {
    if(!children[i].tagName)//有的节点可能是TextNode,无tagName
      continue;
    var tagName=children[i].tagName.toLowerCase();
    if(tagName!="frame" && tagName!="frameset")
      continue;
    if(this.dispType==FrameSplitor.DISPTYPE_HORIZONTAL)
      sizeArr[counter]=children[i].offsetWidth;
    else
      sizeArr[counter]=children[i].offsetHeight;
    counter++;
  }
  return sizeArr;
};

//回调函数
FrameSplitor.prototype.showCallback=function(){};//显示时回调
FrameSplitor.prototype.hideCallback=function(){};//隐藏时回调
FrameSplitor.prototype.moveCallback=function(){};//移动时回调

//显示/隐藏
FrameSplitor.prototype.showHide=function(flag)
{
  if(flag!=null && this.showFlag==flag)
    return;
  if(this.reduceType==FrameSplitor.REDUCE_TYPE_NONE)
    return;
  var imgObj=document.getElementById("mover");
  this.curSizeArr=this.getSizeArr();
  var imgSrc="";
  if(this.showFlag==FrameSplitor.YESNO_YES)//当前显示
  {
    var addSize=0;
    if(this.reduceType==FrameSplitor.REDUCE_TYPE_PRE)
      addSize=this.minSize-this.curSizeArr[this.frameIndex-1];
    else
      addSize=this.curSizeArr[this.frameIndex+1]-this.minSize;
    if(this.dispType==FrameSplitor.DISPTYPE_HORIZONTAL)
    {
      this.frameSetObj.cols=this.getCurRowsCols(addSize);
      if(this.reduceType==FrameSplitor.REDUCE_TYPE_PRE)
        imgSrc="rightMove.gif";
      else
        imgSrc="leftMove.gif";
    }
    else
    {
      this.frameSetObj.rows=this.getCurRowsCols(addSize);
      if(this.reduceType==FrameSplitor.REDUCE_TYPE_PRE)
        imgSrc="downMove.gif";
      else
        imgSrc="upMove.gif";
    }
    this.preSizeArr=this.curSizeArr;
    this.showFlag=FrameSplitor.YESNO_NO;try{
    this.hideCallback();}catch(e){alert(e.message);}
  }
  else
  {
    var addSize=0;
    if(this.reduceType==FrameSplitor.REDUCE_TYPE_PRE)
      addSize=this.preSizeArr[this.frameIndex-1]-this.curSizeArr[this.frameIndex-1];//this.minSize;
    else if(this.reduceType==FrameSplitor.REDUCE_TYPE_POST)
      addSize=this.curSizeArr[this.frameIndex+1]-this.preSizeArr[this.frameIndex+1];
    if(this.dispType==FrameSplitor.DISPTYPE_HORIZONTAL)
    {
      this.frameSetObj.cols=this.getCurRowsCols(addSize);
      if(this.reduceType==FrameSplitor.REDUCE_TYPE_PRE)
        imgSrc="leftMove.gif";
      else
        imgSrc="rightMove.gif";
    }
    else
    {
      this.frameSetObj.rows=this.getCurRowsCols(addSize);
      if(this.reduceType==FrameSplitor.REDUCE_TYPE_PRE)
        imgSrc="upMove.gif";
      else
        imgSrc="downMove.gif";
    }
    
    this.showFlag=FrameSplitor.YESNO_YES;
    this.showCallback();
  }
  imgObj.src=Constants.contextRoot+"../commons/images/public/"+imgSrc;
};

FrameSplitor.prototype.mouseDown=function(ev)
{
  ev || (ev=window.event);//firefox中必须传参数进来。
  this.startScreenX=ev.screenX;
  this.startScreenY=ev.screenY;
  this.curSizeArr=this.getSizeArr();
};
 
FrameSplitor.prototype.mouseUp=function(ev)
{
  this.startScreenX=0;
  this.startScreenY=0;
};

FrameSplitor.prototype.mouseMove=function(ev)
{
  if(this.isLockMove)
    return;
  ev || (ev=window.event);//firefox中必须传参数进来。
  if(Constants.isIE && ev.button!=1)//firefox中如果不按鼠标，不触发onmousemove事件. 且该事件event.button=0
    return;
  if(this.dispType==FrameSplitor.DISPTYPE_HORIZONTAL)
  {
    if(this.startScreenX>0)
    {
      this.frameSetObj.cols=this.getCurRowsCols(ev.screenX-this.startScreenX);
      this.moveCallback();
    }
  }
  else
  {
    if(this.startScreenY>0)
    {
      this.frameSetObj.rows=this.getCurRowsCols(ev.screenY-this.startScreenY);
      this.moveCallback();
    }
  }    
};

//取当前的rows或cols值。
FrameSplitor.prototype.getCurRowsCols=function(addSize)
{
  var rowsCols="";
  for(var i=0;i<this.curSizeArr.length;i++)
  {
    
    if(i>0)
      rowsCols+=",";
    if(this.frameIndex-1==i)
    {
      if(this.isStarArr[i])
        rowsCols+="*";
      else
        rowsCols+=this.curSizeArr[i]+addSize;
      rowsCols+=","+this.curSizeArr[i+1]+",";
      if(this.isStarArr[i+2])
        rowsCols+="*";
      else
        rowsCols+=this.curSizeArr[i+2]-addSize;
      i+=2;
      continue;
    }
    if(this.isStarArr[i])
      rowsCols+="*";
    else
      rowsCols+=this.curSizeArr[i];
  }
  return rowsCols;
};
